package peng.yang.express.VolleyUtils;

import android.util.Log;

import com.sortlistview.CharacterParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import peng.yang.express.Bean.SortModel;

/**
 * Created by Administrator on 2016/6/27.
 */
public class JsonArrayToList {
    JSONObject json = null;
    SortModel sm = null;
    JSONArray ja;

    public JsonArrayToList() {
    }

    public JsonArrayToList(JSONArray ja) {
        this.ja = ja;

    }

    public List<SortModel> setJRToList() {

        List<SortModel> list = new ArrayList<SortModel>();

        for (int i = 0; i < ja.length(); i++) {
            try {
                sm = new SortModel();
                json = ja.getJSONObject(i);
                sm.setName(json.getString("expName"));
                sm.setNum(json.getString("phone"));
                sm.setImageId(json.getString("imgUrl"));
                sm.setSigleName(json.getString("simpleName"));

                //汉字转换成拼音
                String pinyin = CharacterParser.converCnToPY(sm.getName());
                String sortString = pinyin.substring(0, 1).toUpperCase();

                // 正则表达式，判断首字母是否是英文字母
                if (sortString.matches("[A-Z]")) {
                    sm.setSortLetters(sortString.toUpperCase());
                } else {
                    sm.setSortLetters("#");
                }
                list.add(sm);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        Log.e("=========list", list.size() + "");
        return list;


    }

    private void savePicture() {

    }


}